using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Risiko.Logic;
using Risiko.Rendering;
using Risiko.State;
using Risiko.Menu;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.GamerServices;

namespace Risiko.StateManagement
{

  public static class StateManager
  {
    static Supervisor supervisor;
    static TerritoryAssignmentAuto gameRenderer;
    private static StartMenu startMenu;
    //private static MainMenu mainMenu;
    private static Transition transition;

    static public void StartMenu(this Game game)
    {
      game.Cleanup();

      startMenu = new StartMenu(game);
      game.Components.Add(startMenu);

      startMenu.OnStartGame += () => game.Transition();
      startMenu.OnLoadGame += () => game.StartLoadedGame();
    }

    //static public void MainMenu(this Game game)
    //{

    //  mainMenu = new MainMenu(game);
    //  game.Components.Add(mainMenu);

    //  //startMenu = new StartMenu(game);
    //  //game.Components.Add(startMenu);
    //  //startMenu.Disposed = true;
      
    //  mainMenu.OnStartGame += () => game.StartNewGame();      
    //}

    static public void Transition(this Game game)
    {
      game.Cleanup();

      transition = new Transition(game);
      game.Components.Add(transition);

      transition.OnStartGame += () => game.StartNewGame();

    }


    static public void StartGame(this Game game)
    {
      string[] stringList = new string[2] { "Yes", "No" };
      Guide.BeginShowMessageBox("Caricamento", "Vuoi caricare?", stringList, 0, MessageBoxIcon.None, new AsyncCallback(args =>
      {
        var res = Guide.EndShowMessageBox(args);
        if (res == 0)
          StartLoadedGame(game);
        else
          StartMenu(game);
      }), null);

      //Guide.BeginShowKeyboardInput(PlayerIndex.One, "Keyboard", "Caricamento", "Vuoi caricare?", new AsyncCallback(args => 
      //{
      //  var res = Guide.EndShowKeyboardInput(args);
      //  StartLoadedGame(game);
      //}), null);
    }

    static public void StartNewGame(this Game game)
    {
      game.Cleanup();

      gameRenderer = new TerritoryAssignmentAuto(game);

      List<PlayerState> playerStates = new List<PlayerState>();
      playerStates.Add(new PlayerState() { name = "Pippo", color = Color.Red, isHuman = false, HasConquered = false });
      playerStates.Add(new PlayerState() { name = "Pluto", color = Color.Blue, isHuman = false, HasConquered = false });
      playerStates.Add(new PlayerState() { name = "Paperoga", color = Color.Yellow, isHuman = true, HasConquered = false });
      playerStates.Add(new PlayerState() { name = "Paperino", color = Color.Green, isHuman = false, HasConquered = false });
      //playerStates.Add(new PlayerState() { name = "Topolino", color = Color.Black, isHuman = false, HasConquered = false });
      //playerStates.Add(new PlayerState() { name = "Gastone", color = Color.Violet, isHuman = false, HasConquered = false });

      //playerStates[0].mission = {playerStates[0].TerritoryRef.Count() == 24};

      supervisor = new Logic.Supervisor(game, playerStates);

      game.Components.Add(supervisor);
      game.Components.Add(gameRenderer);
    }


    static public void StartLoadedGame(this Game game)
    {
      game.Cleanup();

      gameRenderer = new TerritoryAssignmentAuto(game);

      State.State loadedState = StateManagement.SaveUtils.LoadGame();

      List<PlayerState> playerStates = loadedState.playerStates.ToList();
      supervisor = new Logic.Supervisor(game, playerStates);

      game.Components.Add(supervisor);
      game.Components.Add(gameRenderer);
    }


    static private void Cleanup(this Game game)
    {
      for (int i = 0; i < game.Components.Count; i++)
        ((GameComponent)game.Components[i]).Dispose();
    }
  }
}
